import fetch from 'node-fetch'
import { DB_URL } from '../common/config.js'

const request = async (action: string, body: any = {}): Promise<any> => {
  const url = `${DB_URL}/${action}`
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(body),
    })
    if (response.ok) { 
      const json: any = await response.json() 
      if (json.code === 200) {
        return json.data
      }
      return json.message
    } else {
      return `错误码：${response.status}`
    }
  } catch (error: any) {
    return error.message
  }
}

export default request
